<?php

declare(strict_types=1);

namespace app\admin\controller;

use think\App;
use app\common\builder\TableBuilder;
use app\common\builder\TableFront;

use think\facade\View;
use think\response\Json;

use app\common\model\CheckListDetails;
use app\common\model\CheckList;

class ProTopic extends Base
{

    //主键
    protected $pk = 'id';

    //主模型
    protected $model = '\app\common\model\ProTopic';

    //关联模型
    protected $hasOne = ['pro', 'procate'];

    public function index()
    {
        //CheckList::where([['id','>',0]])->update(['is_del'=>0]);
        return $this->setTable();
    }

    //自动构建table,按需修改
    public function setTable()
    {
        $tableFront = TableFront::getInstance()
            ->addRow(['type' => 'checkbox', 'fixed' => 'left', 'field' => '', 'title' => '全选'])
            ->addRow(['field' => 'id', 'sort' => true, 'title' => 'ID', 'width' => 80])
            ->addRow(['field' => 'title', 'title' => '题目', 'modify' => 'add|edit', 'format' => 'input'])
            ->addRow(['field' => 'protitle', 'title' => '所属项目', 'templet' => '<div>{{d.pro.title}}</div>'])
            ->addRow(['field' => 'catetitle', 'title' => '所属分类', 'templet' => '<div>{{d.procate.title}}</div>'])
            ->addRow(['field' => 'score', 'title' => '分数', 'modify' => 'add|edit', 'format' => 'number'])
            ->addRow(['field' => 'sort', 'title' => '排序', 'modify' => 'add|edit', 'format' => 'number'])
            ->addRow(['field' => 'create_time', 'title' => '创建时间'])
            ->addRow(['field' => 'update_time', 'title' => '更新时间'])
            ->addRow(['fixed' => 'right', 'align' => 'center', 'toolbar' => '#barDemo', 'title' => '操作', 'width' => 200]);

        return TableBuilder::getInstance()
            ->addColumns($tableFront)
            ->addRightButtons(['edit' => ['width' => 1000, 'height' => 1000], 'delete'])
            ->addTopButtons(['add' => ['width' => 1000, 'height' => 1000]])
            ->setSearch($this->setSearch())
            ->fetch('protopic/index');
    }
    //自动构建Search，按需修改
    public function setSearch()
    {
        return TableFront::getInstance()
            ->setSearch(['title', '题目', 'input', '', '', '', 'like'])
            ->setSearch(['pro_id,cate_id', '所属项目,所属分类', 'linkage', url('ProCate/getCate')->build()]);
    }

    public function add($name = '', $tpl = 'protopic/edit')
    {
        View::assign('sort', ($this->model::where(array('is_del' => 0))->max('sort') + 1));
        return parent::add($name, $tpl); // TODO: Change the autogenerated stub
    }

    public function edit($name = '', $id = '', $tpl = 'protopic/edit', $info = '')
    {
        $info = $this->model::find($id);
        return parent::edit($name, $id, $tpl, $info); // TODO: Change the autogenerated stub
    }

    //导入为excel文件
    public function importcompanyexcel(): Json
    {
        $param = $this->request->param();
        $data = htmlspecialchars_decode($param['persons']);
        $list = json_decode($data, true);
        //print('<pre>');print_r($list);die;
        foreach ($list as $k => $v) {
            //$find = $this->model::where(['title' => $v['题目'],'is_del'=>0])->find();

            $sort = $this->model::where(['is_del' => 0])->max('sort');
            // if (empty($find)) {
            $companydata['title'] = $v['题目'];
            $companydata['pro_id'] = $v['所属项目（检查类别项目id）'];
            $companydata['desc'] = isset($v['题目介绍']) && !empty($v['题目介绍']) ? $v['题目介绍'] : "";
            $companydata['cate_id'] = $v['分类'];
            $companydata['score'] = $v['分数'];
            $companydata['sort'] = $sort + 1;
            $companydata['is_del'] = 0;
            $this->model::create($companydata);
            // }
        }
        return json(['code' => 1, 'msg' => '导入成功！']);
    }
    /**
     * 原点 2021年7月27日14:46:29 公共删除方法
     */
    public function companydel(): Json
    {
        try {
            $id = $this->request->post('id');

            $model = $this->model::where('id', $id)->find();
            $model->is_del = 1;
            $model->save();
            // $list = $this->model::order('id asc')->select();

            // foreach ($list as $k => $v) {
            //     CheckList::where(['pro_id' => $v['pro_id']])->update(['is_del' => $v['is_del']]);
            //     CheckListDetails::where(['topic_id' => $v['id']])->update(['is_del' => $v['is_del']]);
            // }
            return json(['code' => 1, 'msg' => '操作成功']);
        } catch (\Exception $e) {
            return json(['code' => -1, 'msg' => $e->getMessage()]);
        }
    }

    /**
     * 原点 2021年7月27日14:46:29 公共删除方法
     */
    public function companyAlldel(): Json
    {
        try {
            $id = $this->request->post('ids');
            $ids = explode(',', $id);
            foreach ($ids as $v) {
                $model = $this->model::where('id', $v)->find();
                $model->is_del = 1;
                $model->save();
            }
            // $list = $this->model::where(['is_del' => 1])->order('id asc')->select();
            // if (!empty($list)) {
            //     foreach ($list as $k => $v) {
            //         CheckList::where(['pro_id' => $v['pro_id']])->update(['is_del' => 1]);
            //         CheckListDetails::where(['topic_id' => $v['id']])->update(['is_del' => 1]);
            //        // print_r(111);die;
            //     }
            // }
            return json(['code' => 1, 'msg' => '操作成功']);
        } catch (\Exception $e) {
            return json(['code' => -1, 'msg' => $e->getMessage()]);
        }
    }
}
